package com.mail.portal.modular.controller;

import com.mail.portal.common.controller.BaseController;
import com.mail.portal.modular.model.News;
import com.mail.portal.modular.model.Product;
import com.mail.portal.modular.model.ProductCategory;
import com.mail.portal.modular.service.IProductCategoryService;
import com.mail.portal.modular.service.IProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


/**
 * 创建人： Shi Heng
 * 创建时间：2018-04-11 12:19
 * QQ：374696376
 * Description：
 */
@Controller
@RequestMapping("/jhcb/product")
public class ProductController extends BaseController {

    @Resource
    private IProductCategoryService productCategoryService;

    @Resource
    private IProductService productService;

    @RequestMapping("/category")
    @ResponseBody
    public List<ProductCategory> category(){
        return productCategoryService.selectAll();
    }


    @RequestMapping("/product_list.html")
    public String productEdit(@RequestParam(value = "title",required = false) String title,
                              @RequestParam(value = "beginTime",required = false) String beginTime,
                              @RequestParam(value = "endTime",required = false) String endTime,
                              @RequestParam(value = "product_category_id",required = false) Integer product_category_id,
                              Model model) {
        List<Product> products = productService.selectProducts(title,beginTime,endTime,product_category_id);
        model.addAttribute("products", products);
        List<ProductCategory> categories = productCategoryService.selectAll();
        model.addAttribute("categories", categories);
        return "/product_list.html";
    }

    @RequestMapping("/product/{category}.html")
    public String productCategory(@PathVariable(value = "category") Integer category,Model model) {
        List<Product> products = productService.selectProducts(null,null,null,0==category?null:category);
        model.addAttribute("products", products);
        List<ProductCategory> categories = productCategoryService.selectAll();
        model.addAttribute("categories", categories);
        return "/product_list.html";
    }

    @RequestMapping("/product_detail/{id}.html")
    public String newsDetail(@PathVariable("id") Integer id, Model model) {
        Product product = productService.selectByPrimaryKey(id);
        model.addAttribute("product", product);
        List<ProductCategory> categories = productCategoryService.selectAll();
        model.addAttribute("categories", categories);
        return "/product.html";
    }

}
